﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Engine.Components.Workers
{
    public class FileInfoCRC 
    {
        System.IO.FileInfo _fileInfo;
        public System.IO.FileInfo FileInfo 
        { 
            get
            {
                return _fileInfo;
            }

            set
            {
                _fileInfo = value;
            }
        }
        private long _crc;
        public long CRC {
            get { return _crc;}
            set { _crc = value; }
        }

        public override string ToString()
        {
            return base.ToString() + " for " + FileInfo.Name + " with CRC = " + CRC.ToString();
        }
    }
    public class FileInfoProducer:Abstract.WorkerBase
    {
        #region PRIVATE
        IO.CRC32 _CRCCalculator = new IO.CRC32();
        #endregion
        
        #region FACTORY
        private FileInfoProducer(HunterPluginSDK.Interface.IProgressHandler ph)
        {
            this.ProgressHandler = ph;
        }
        static public Engine.Components.Interface.IWorker CreateWorker(HunterPluginSDK.Interface.IProgressHandler ph)
        {
            return new FileInfoProducer(ph);
        }
        #endregion

        protected override void DoMyJob(object o)
        {
            try
            {
                FileInfoCRC fie = new FileInfoCRC();
                fie.FileInfo = o as System.IO.FileInfo;
                System.IO.FileStream fs = fie.FileInfo.OpenRead();
                fie.CRC = _CRCCalculator.CRC(fs);
                fs.Close();
                this.OnProduced(fie);
            }
            catch (Exception)
            { 
            }
        }
    }
}
